//
//  RentalViewController.m
//  Item A House
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RentalViewController.h"
#import "RentalModel.h"
#import "RentalTableViewCell.h"
#import "ConnectModel.h"
#import "UIImageView+WebCache.h"
#import "rTwoViewController.h"
#import "PendulumView.h"
@interface RentalViewController ()<UITableViewDataSource, UITableViewDelegate, ConnectModelDelegate>
@property (nonatomic, retain) ConnectModel *conn;
@property (nonatomic, retain) PendulumView *pend;
@end

@implementation RentalViewController
- (void)dealloc
{
    _conn.delegate = nil;
    [_conn release];
    [_table release];
    [_array release];
    [_pend release];
    [super dealloc];
}
//http://m.goufang.com/api/zufang/zflist
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        
        
    }
    return self;
}
- (void)connectFinished:(NSData *)data
{
    NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@", dic);
    
    NSMutableArray *array = [dic objectForKey:@"Table"];
    for (NSMutableDictionary *dic in array) {
        RentalModel *model = [[RentalModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.array addObject:model];
    }
    [self.table reloadData];
    [self.pend removeFromSuperview];
}
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.rowHeight = 100;
    [self.view addSubview:self.table];
    [self.table release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
//tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"李狗蛋";
    RentalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[[RentalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentify]autorelease];
    }
    RentalModel *model = [self.array objectAtIndex:indexPath.row];
    cell.addrinfo.text = model.addrinfo;
    cell.title.text = model.title;
    cell.smallareaid.text = model.smallareaid;
    cell.v_houselayout.text = model.v_houselayout;
    cell.fitment.text = model.fitment;
    cell.v_price.text = [NSString stringWithFormat:@"%ld元/月", (long)model.v_price];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    rTwoViewController *rTwo = [[rTwoViewController alloc] init];
    RentalModel *model = [self.array objectAtIndex:indexPath.row];
    rTwo.hid_id = model.houseid;
    
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:rTwo animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
    [rTwo release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    NSDictionary *dic = @{@"cityid":@"dl", @"page":@"1"};
    self.conn = [ConnectModel connectWithUrl:@"http://m.goufang.com/api/zufang/zflist" delegate:self parmaters:dic];
    [self createTable];
    
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    self.pend = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:_pend];
    // Do any additional setup after loading the view.
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
}
-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
